﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BenefitHelper.Sys
{
    /// <summary>
    /// 不是交易员，但是可以使用这个系统的人
    /// </summary>
    public class User
    {
        public int Id { get; set; }

        public string Username { get; set; }

        public string Password { get; set; }

        public string NickName { get; set; }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int[] Login(string username, string password, int tradeHistoryId, string pdate, DB.DBManager db)
        {
            int[] userinfo = new int[2];
            userinfo[0] = -1;
            userinfo[1] = -1;
            var query = db.User.Where(a => a.Username.Equals(username)).Where(a => a.Password.Equals(password));

            if (query.Count() == 0)//操作员登录
            {
                int operatorid = new Sys.Operator().OperatorLogin(username, password, tradeHistoryId, pdate, db);
                if (operatorid != 0)
                {
                    userinfo[0] = operatorid;
                    userinfo[1] = 1;
                }

            }
            else//管理员登录
            {
                userinfo[0] = UserLogin(query.First(), tradeHistoryId, pdate);
                userinfo[1] = 2;
            }
            return userinfo;
        }
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int UserLogin(User user, int tradeHistoryId, string pdate)
        {
            int id = user.Id;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            cookie.SetCookie("LoginUserId", id.ToString(), 4);
            cookie.SetCookie("LoginUserType", Convert.ToInt32(Config.UserType.Master).ToString(), 4);
            cookie.SetCookie("TradingDayId", tradeHistoryId.ToString(), 4);
            cookie.SetCookie("TradingDayPdate", pdate, 4);
            return id;
        }

        public List<User> GetList(DB.DBManager db)
        {
            return db.User.ToList();
        }

        public User GetModel(int id, DB.DBManager db)
        {
            return db.User.Find(id);
        }
    }
}